#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //初始化输入框
    ui->lineEdit->setPlaceholderText("请输入文本");
    ui->lineEdit->setClearButtonEnabled(true);

    //新增初始列
    ui->tableWidget->insertColumn(0);
    ui->tableWidget->insertColumn(1);
    ui->tableWidget->insertColumn(2);
    //设置列名
    ui->tableWidget->setHorizontalHeaderItem(0,new QTableWidgetItem("姓名"));
    ui->tableWidget->setHorizontalHeaderItem(1,new QTableWidgetItem("年纪"));
    ui->tableWidget->setHorizontalHeaderItem(2,new QTableWidgetItem("学号"));
    //新增初始列
    ui->tableWidget->insertRow(0);
    ui->tableWidget->insertRow(1);
    ui->tableWidget->insertRow(2);
    //增加元素
    ui->tableWidget->setItem(0,0,new QTableWidgetItem("张三"));
    ui->tableWidget->setItem(1,0,new QTableWidgetItem("李四"));
    ui->tableWidget->setItem(2,0 ,new QTableWidgetItem("王五"));

    ui->tableWidget->setItem(0,1, new QTableWidgetItem("1"));
    ui->tableWidget->setItem(1,1,new QTableWidgetItem("2"));
    ui->tableWidget->setItem(2,1,new QTableWidgetItem("3"));

    ui->tableWidget->setItem(0,2,new QTableWidgetItem("1111"));
    ui->tableWidget->setItem(1,2,new QTableWidgetItem("3333"));
    ui->tableWidget->setItem(2,2,new QTableWidgetItem("22222"));





}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_new_column_clicked()
{

  const QString& tmp = ui->lineEdit->text();
  if(tmp.isEmpty())
  {
      //拿不到文本就直接返回
      return;
  }
  //在这里拿到的是总共的行数,创建行的时候是下标,最近的一个应该是column-1,新建就是这个数
  int column = ui->tableWidget->columnCount();
  ui->tableWidget->insertColumn(column);
  ui->tableWidget->setHorizontalHeaderItem(column,new QTableWidgetItem(tmp));


}

void Widget::on_pushButton_new_row_clicked()
{
    //同上
    int row = ui->tableWidget->rowCount();
    ui->tableWidget->insertRow(row);
}

void Widget::on_pushButton_delete_column_clicked()
{
    int column = ui->tableWidget->currentColumn();
    ui->tableWidget->removeColumn(column);
}

void Widget::on_pushButton_delete_row_clicked()
{
    int row = ui->tableWidget->currentRow();
    ui->tableWidget->removeRow(row);
}
